package com.cy.pj.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Aspect
//@Component
public class SysCacheAspect {
    private Map<String,Object> cache=new ConcurrentHashMap<>();

    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")
    public void doCache() {}

    @Pointcut("@annotation(com.cy.pj.common.annotation.ClearCache)")
    private void doClearCache(){}

    @AfterReturning("doClearCache()")
    public void doAfterReturning(){
        cache.clear();
    }

    @Around("doCache()")
    public Object around(ProceedingJoinPoint jp)
            throws Throwable{
        System.out.println("Get data from cache");
        Object result=cache.get("deptKey");
        if (result!=null)return result;
        result= jp.proceed();
        System.out.println("Put data to cache");
        cache.put("deptKey",result);
        return result;
    }
}
